

#define volume_max 8   //最大音量
#define volume_min 1   //最小音量
#define bell_start 1   //铃声起始索引
#define bell_end 3     //铃声结束索引
#define remind_max 10  //最大提醒次数
#define remind_min 1   //最小提醒次数

#define remind_default 2  //默认提醒次数
#define bell_default 1    //默认提醒铃声
#define volume_default 1  //默认音量大小

#define Product_Key "d7f3&24aS#fgh_24x8f-9b41q5m"
#define Product_ID "gh_137006337a8b"

#ifndef TEST 
#define Pro_topic "jzl"  		//一级主题 产品
#else
#define Pro_topic "jzl_"  		//一级主题 产品
#endif
#define DevType_topic "1"  		//二级主题 设备类型
#define DevVers_topic "1"  		//三级主题 设备版本
#define ObjSys_topic "sys"  	//四级主题 发往对象  服务器
#define EveConn_topic "c"  		//五级主题 上线事件
#define EveData_topic "d"  		//五级主题 数据事件
#define EveLwt_topic "l"  		//五级主题 离线事件

#define Add_Host_Ack 1    //添加主机应答
#define Del_Host_Ack 2    //删除主机应答
#define Add_Exte_Ack 3    //添加分机应答
#define Mod_Exte_Ack 4    //修改分机应答
#define Del_Exte_Ack 5    //删除分机应答
#define Call_Exte 6       //分机呼叫
#define Req_Time 7        //请求时间
#define Call_BLE_Exte 14  // BLE分机呼叫
#define Reg_Exte 15       // BLE分机注册

#define Add_Host 8       //添加主机
#define Del_Host 9       //删除主机
#define Add_Exte 10      //添加分机
#define Mod_Exte 11      //修改分机
#define Del_Exte 12      //删除分机
#define Upd_Time 13  	 //更新时间
#define ClrDisp_Exte 16  //清除显示主机

//网络设备类型
enum {
  net_device_none = 0,  //无网络设备
  net_device_wifi,      // Wifi
  net_device_gsm,       // gsm
};

//网络设备状态
typedef enum {
  net_idle = 0,  //空闲
  net_doing      //工作
} eNetStatus;

//芯片uid
typedef __packed struct {
  unsigned int temp0;
  unsigned int temp1;
  unsigned int temp2;
} sUID;

typedef __packed union {
  sUID Val;
  unsigned char Var[12];
} uUID;

//系统配置信息存储结构
typedef __packed struct {
  unsigned char Device_id[17];    //设备ID
  uUID Device_mac;                //设备mac
  unsigned char SysInitFlag : 2;  //系统初始化标志
  unsigned char MacFlag : 2;      // MAC添加标志
  unsigned char WifiFlag : 2;     // Wifi模块配置标志
  unsigned char DeviceFlag : 2;   //设备ID获取标志
  unsigned char AddFlag : 2;      //主机添加标志
  unsigned char Flash_index : 1;  // flash区块记录标志
  unsigned char RemindCnt : 4;    //提醒次数
  unsigned char BellIndex : 3;    //提醒铃声
  unsigned char Volume : 4;       //设置音量大小
  unsigned char Retain : 2;       //保留

} tCfg;

////系统配置信息存储结构
// typedef  __packed struct
//{
//	unsigned char		Device_id[17];
////设备ID 	uUID				Device_mac;
////设备mac 	unsigned char 		SysInitFlag 	;
////系统初始化标志 	unsigned char		MacFlag 		;
////MAC添加标志 	unsigned char		WifiFlag 		;
////Wifi模块配置标志 	unsigned char 		DeviceFlag 		;
////设备ID获取标志 	unsigned char		AddFlag			;
////主机添加标志 	unsigned char		Flash_index		;
////flash区块记录标志 	unsigned char 		RemindCnt		;
////提醒次数 	unsigned char		BellIndex		;
////提醒铃声 	unsigned char		Volume			;
////设置音量大小

//}tCfg;

typedef union sCfgData {
  tCfg Val;
  unsigned char Var[32];
} tCfgData;

enum {
  Sys_NotInit = 3,  //未初始化
  Sys_Init = 2      //已初始化
};

enum {
  Mac_NotInit = 3,  // Mac未添加
  Mac_Init = 2      // Mac已添加
};

enum {
  Wifi_NotInit = 3,  // wifi已配置
  Wifi_Init = 2      // Wifi未配置
};

enum {
  Device_NotInit = 3,  //设备已配置
  Device_Init = 2,     //设备未配置
};

enum {
  flash1 = 0,  // flash1区
  flash2       // flash2区
};

enum {
  NoneAdd = 2,  //主机未添加
  HaveAdd = 1,  //主机已经添加
};

// mqtt推送数据格式
typedef struct {
  unsigned char eve;
  unsigned char cmd;
  unsigned char dup;
  unsigned short msgid;
  void *data;
} sMqttData;

//设置功能
enum {
  f_start = 0,
  f_voice,   //设置声音
  f_bell,    //铃声设置
  f_remind,  //呼叫次数提醒
  f_add,     //分机添加
  f_del,     //分机删除
  f_time,    //时间设置
  f_net,     //网络设置
};

//设置菜单
typedef struct {
  unsigned char *menu;
  unsigned char count;

} sSetMenu;

//功能操作
typedef struct {
  unsigned short ctrl : 5;     //操作
  unsigned short fun : 5;      //功能
  unsigned short cmd : 4;      //命令
  unsigned short reserve : 2;  //保留
} sEnterMenu;

// sEnterMenu:ctrl
enum {
  ctrl_none = 0,
  ctrl_voice,
  ctrl_fun,
  ctrl_setbell,
  ctrl_setremind,
  ctrl_time,
  ctrl_add,
  ctrl_del,
  ctrl_net,
  ctrl_delallnum,
  ctrl_num,
};

// sEnterMenu:cmd
enum {
  cmd_none = 0,
  cmd_enter,   //进入操作
  cmd_back,    //返回操作
  cmd_up,      //上翻操作
  cmd_down,    //下翻操作
  cmd_switch,  //选位操作
  cmd_confirm,
  cmd_longconf,
  cmd_longback,
};

enum {
  Led_None = 0,
  Led_DevInit,
  Led_DevWifi,
  Led_DevGsm,
  Led_DevSingle,
  Led_Call,
  Led_ClearCall,
  Led_SetOk,
  Led_SetNet,
  Led_SetNetOk,
  Led_SetNetFailed,
  Led_NoConnect,
  Led_Connect,
};

typedef enum sModel {
  Model_Call = 0x01,  //呼叫模式
  Model_Add,          //添加分机模式
  Model_Del,          //删除分机模式
} tModel;

#define topic_eve_conn			0			//mqtt上线事件
#define topic_eve_data			1			//mqtt数据事件

//从机标志结构
typedef __packed union sSlave {
  unsigned char Var[2];
  unsigned short Val;
} tSlave;

typedef __packed struct {
  unsigned int temp0;
  unsigned int temp1;
  unsigned int temp2;
} iSMacSn;

typedef __packed union {
  iSMacSn Mac;
  unsigned char Mac_Var[12];
} uSMacSn;

//通讯协议帧结构
typedef __packed struct sCmd {
  unsigned char Head;     //帧头
  unsigned char Command;  //命令
  iSMacSn Slave;          //从机MAC
  unsigned char Crc;      //校验
  unsigned char Tail;     //帧尾
} tCmd;

#define Sentdata_len 16
//收发数据结构
typedef __packed union sSend_Data {
  tCmd Cmd;
  unsigned char Var[Sentdata_len];
} tSend_Data;

// flash存储数据包
typedef __packed struct {
  unsigned char valid;
  iSMacSn mac;
  tSlave slave;

} sFlash;

typedef __packed union {
  sFlash Val;
  unsigned char Var[15];
} sFlashPack;

//呼叫器号码显示队列结构
typedef __packed struct sListNum {
  struct sListNum *next;
  tSlave slave;           //从机号码
  unsigned char time;     //显示时间  单位秒
  unsigned char cnt;      //显示次数
  unsigned char is_disp;  //播放标志
} tListNum;

//呼叫器号码显示队列结构
typedef __packed struct sListDispNum {
  struct sListDispNum *next;
  tSlave slave;        //从机号码
  unsigned char time;  //显示时间  单位秒
  unsigned char cnt;   //显示次数
} tListDispNum;

typedef union {
  struct {
    unsigned char keydog : 1;
    unsigned char loradog : 1;
    unsigned char reserve : 6;
  } bit;
  unsigned char flag;
} uFeedDog;

//位置信息
typedef struct {
  unsigned char mac[6];
} Location;

// st设备呼叫包
typedef struct {
  iSMacSn mac;
  unsigned char power;
} PackageST;

// ble设备呼叫包
typedef struct {
  iSMacSn mac;
  unsigned char power;
  Location location;
} PackageBLE;

typedef struct sMacCall {
  // unsigned char mac[6];
  struct sMacCall *next;
  iSMacSn mac;
  unsigned char power;
  unsigned char timeout;
} tMacCall;

extern struct process DevCheck_Process;
extern tCfgData cfg_data;
extern sUID uid;
extern volatile eNetStatus netstatus;
extern unsigned char netdevice_type;

extern unsigned char led_data[];  //数码管显示数组

extern sSetMenu SetMenu;  //设置菜单的模式（初始化、单机wifi或gprs）

extern tModel Model;

void exit_wifi(void);
void exit_gprs(void);
void exit_mqtt(void);

void write_cfg(tCfgData *cfgdata);
void menb_pack_init(void);
void send_callnum(PackageST st_package);
void send_callnum_location(PackageBLE ble_package);
void send_register(PackageST st_package);
void Feed_KeyDog(void);
void Feed_LoraDog(void);
unsigned char Get_FeedDog(void);
void Clear_FeedDog(void);
unsigned char FeedDog_Init(void);
